<?php

/**
 * This is the model class for table "yii_przychodnia.pacjent".
 *
 * The followings are the available columns in table 'yii_przychodnia.pacjent':
 * @property integer $idPacjent
 * @property string $Imie
 * @property string $Nazwisko
 * @property string $Pesel
 * @property string $Telefon
 * @property string $Email
 *
 * The followings are the available model relations:
 * @property Wizyta[] $wizytas
 */
class Pacjent extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Pacjent the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'yii_przychodnia.pacjent';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('Pesel', 'required'),
                        array('Imie, Nazwisko, Email', 'required', 'on'=>'newPacjent'),
			array('Imie, Telefon', 'length', 'max'=>15),
			array('Nazwisko', 'length', 'max'=>45),
			array('Pesel', 'length', 'min'=>11, 'max'=>11),
                        array('Pesel', 'checksum'),
			array('Email', 'length', 'max'=>60),
                        array('Email', 'email'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('idPacjent, Imie, Nazwisko, Pesel, Telefon, Email', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'wizytas' => array(self::HAS_MANY, 'Wizyta', 'idPacjent'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'idPacjent' => 'Id Pacjent',
			'Imie' => 'Imię',
			'Nazwisko' => 'Nazwisko',
			'Pesel' => 'PESEL',
			'Telefon' => 'Numer telefonu',
			'Email' => 'Adres e-mail',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('idPacjent',$this->idPacjent);
		$criteria->compare('Imie',$this->Imie,true);
		$criteria->compare('Nazwisko',$this->Nazwisko,true);
		$criteria->compare('Pesel',$this->Pesel,true);
		$criteria->compare('Telefon',$this->Telefon,true);
		$criteria->compare('Email',$this->Email,true);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}

        /**
	 * Calculates checksum for PESEL number.
	 * This is the 'checksum' validator as declared in rules().
	 */
	public function checksum($attribute,$params=NULL)
	{
		if(!$this->hasErrors($attribute) && strlen($this->Pesel) == 11)
		{
			$pesel = str_split($this->Pesel);
                        $checksum = 0;
                        $n = array(1, 3, 7, 9, 1, 3, 7, 9, 1, 3);
                        for ($i=0; $i<=9; $i++) {
                            $checksum += $n[$i] * $pesel[$i];
                        }
                        if ((10-($checksum%10))%10 != $pesel[10])
                            $this->addError('Pesel','Numer jest nieprawidłowy.');
                }
	}

        public function getFullname()
        {
                return $this->Nazwisko.' '.$this->Imie;
        }
}